package com.mixsmart.commonweb.dao;

import java.util.ArrayList;
import java.util.Date;

import com.mixsmart.commonweb.bean.ICreateTime;
import com.mixsmart.commonweb.bean.IPKStringBean;
import com.mixsmart.commonweb.bean.IPrefix;
import com.mixsmart.core.bean.entity.IEntityBean;
import com.mixsmart.core.dao.impl.BaseEntityDao;
import com.mixsmart.spellsql.utils.Cnd;
import com.mixsmart.spellsql.utils.Exp;
import com.mixsmart.utils.ArrayUtils;
import com.mixsmart.utils.CollectionUtils;
import com.mixsmart.utils.StringUtils;

/**
 * 主键为字符串的DAO；
 * 所有主键为字符串的实体bean对应的DAO必须继承该类
 * @author lmq
 * @version 1.0
 * @since JDK版本大于等于1.6
 * 2016年1月26日
 * @param <T>
 */
public abstract class PKStringDao<T extends IPKStringBean> extends BaseEntityDao<T, String>{

	@Override
	public boolean save(T t) {
		if(null != t ) {
			if(t instanceof IPKStringBean && StringUtils.isEmpty(t.getId())) {
				String id = StringUtils.uuid();
				if(t instanceof IPrefix) {
					String prefix = ((IPrefix) t).getPrefix();
					if(StringUtils.isNotEmpty(prefix)) {
						id = prefix.toUpperCase()+"_"+id;
					}
				}
				t.setId(id);
			}
			if(t instanceof ICreateTime && null == ((ICreateTime)t).getCreateTime()) {
				((ICreateTime)t).setCreateTime(new Date());
			}
		}
		return super.save(t);
	}

	@Override
	public boolean save(ArrayList<T> ts) {
		if(CollectionUtils.isNotEmpty(ts)) {
			for (T t : ts) {
				if(t instanceof IPKStringBean && StringUtils.isEmpty(t.getId())) {
					String id = StringUtils.uuid();
					if(t instanceof IPrefix) {
						String prefix = ((IPrefix) t).getPrefix();
						if(StringUtils.isNotEmpty(prefix)) {
							id = prefix.toUpperCase()+"_"+id;
						}
					}
					t.setId(id);
				}
				if(t instanceof ICreateTime && null == ((ICreateTime)t).getCreateTime()) {
					((ICreateTime)t).setCreateTime(new Date());
				}
			}
		}
		return super.save(ts);
	}

	@Override
	public boolean saveOrUpdate(T t) {
		if(null != t ) {
			if(t instanceof IPKStringBean && StringUtils.isEmpty(t.getId())) {
				String id = StringUtils.uuid();
				if(t instanceof IPrefix) {
					String prefix = ((IPrefix) t).getPrefix();
					if(StringUtils.isNotEmpty(prefix)) {
						id = prefix.toUpperCase()+"_"+id;
					}
				}
				t.setId(id);
			}
			if(t instanceof ICreateTime && null == ((ICreateTime)t).getCreateTime()) {
				((ICreateTime)t).setCreateTime(new Date());
			}
		}
		return super.saveOrUpdate(t);
	}

	@Override
	public boolean saveObj(IEntityBean bean) {
		if(null != bean ) {
			if(bean instanceof IPKStringBean && StringUtils.isEmpty(((IPKStringBean)bean).getId())) {
				String id = StringUtils.uuid();
				if(bean instanceof IPrefix) {
					String prefix = ((IPrefix) bean).getPrefix();
					if(StringUtils.isNotEmpty(prefix)) {
						id = prefix.toUpperCase()+"_"+id;
					}
				}
				((IPKStringBean)bean).setId(id);
			}
			if(bean instanceof ICreateTime && null == ((ICreateTime)bean).getCreateTime()) {
				((ICreateTime)bean).setCreateTime(new Date());
			}
		}
		return super.saveObj(bean);
	}

	@Override
	public boolean saveObj(ArrayList<? extends IEntityBean> beans) {
		if(CollectionUtils.isNotEmpty(beans)) {
			for (IEntityBean bean : beans) {
				if(bean instanceof IPKStringBean && StringUtils.isEmpty(((IPKStringBean)bean).getId())) {
					String id = StringUtils.uuid();
					if(bean instanceof IPrefix) {
						String prefix = ((IPrefix) bean).getPrefix();
						if(StringUtils.isNotEmpty(prefix)) {
							id = prefix.toUpperCase()+"_"+id;
						}
					}
					((IPKStringBean)bean).setId(id);
				}
				if(bean instanceof ICreateTime && null == ((ICreateTime)bean).getCreateTime()) {
					((ICreateTime)bean).setCreateTime(new Date());
				}
			}
		}
		return super.saveObj(beans);
	}

	@Override
	public boolean saveOrUpdateObj(IEntityBean bean) {
		if(null != bean ) {
			if(bean instanceof IPKStringBean && StringUtils.isEmpty(((IPKStringBean)bean).getId())) {
				String id = StringUtils.uuid();
				if(bean instanceof IPrefix) {
					String prefix = ((IPrefix) bean).getPrefix();
					if(StringUtils.isNotEmpty(prefix)) {
						id = prefix.toUpperCase()+"_"+id;
					}
				}
				((IPKStringBean)bean).setId(id);
			}
			if(bean instanceof ICreateTime && null == ((ICreateTime)bean).getCreateTime()) {
				((ICreateTime)bean).setCreateTime(new Date());
			}
		}
		return super.saveOrUpdateObj(bean);
	}

	@Override
	public boolean delete(String[] ids) {
		boolean is = false;
		if(ArrayUtils.isNotEmpty(ids)) {
			is = super.delete(Cnd.where(Exp.in("id", ids)));
		}
		return is;
	}
	
}
